package com.skytech.component.crawler.special;

import com.alibaba.fastjson.JSONObject;
import com.skytech.component.crawler.parse.SaveDataToES;
import com.skytech.component.crawler.util.ChromeBrowser;
import com.skytech.component.crawler.util.ElementAbstractUrl;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;



@Service
public class ShanghaiSec {

    @Autowired
    private ChromeBrowser chromeBrowser;

    @Autowired
    private SaveDataToES saveDataToES;

    private static final Logger log = LoggerFactory.getLogger(ShanghaiSec.class);


    public void start() {

        ChromeDriver driver = chromeBrowser.getChromeDriver();
        try {
            driver.get("http://www.sse.com.cn/disclosure/listedinfo/regular/");//打开网页
            Thread.sleep(3000);
            Elements elements=null;
            partClickNext(driver,elements);
        } catch (Exception e) {
            log.error("{}--网页打开失败--{}", driver.getCurrentUrl(), e.getMessage());
        } finally {
            driver.quit();
            log.info("数据爬取结束");
        }
    }

    private void partClickNext(ChromeDriver driver,Elements elements) {
        String htmlNew = driver.findElementByTagName("html").getAttribute("outerHTML");

        Document parse = Jsoup.parse(htmlNew, driver.getCurrentUrl());
   /*     if (html != null) {
            if (html.equals(htmlNew)) {
                log.error("两次链接内容相同,当前页{}", driver.getCurrentUrl());
                return;
            }
        }
        html = htmlNew;*/
        Elements allElements = parse.getAllElements();

        Elements elementsNew=allElements.select("#tabs-658545 > div.sse_list_1.list > dl> dd");
        if(elements!=null){
            if(elements.equals(elementsNew)){
                log.error("两次链接内容相同,当前页{}", driver.getCurrentUrl());
                return;
            }
        }
        elements=elementsNew;
      // ElementAbstractUrl.links(elements);
        for (Element e: elements) {
            JSONObject jsonObject=new JSONObject();
           jsonObject.put("publish_time", e.getElementsByTag("span").text());
           jsonObject.put("file_source", ElementAbstractUrl.links(e.getElementsByTag("a")).get(0));
           jsonObject.put("id",e.getElementsByTag("a").text());
           jsonObject.put("file_name",e.getElementsByTag("a").text());
           jsonObject.put("code",e.getElementsByTag("a").text().split(":")[0]);
            jsonObject.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
           //jsonObject.put("company_name",e.getElementsByTag("a"))
            jsonObject.put("dataType","sec");
            jsonObject.put("sourceType","shanghai");
            jsonObject.put("sourceUrl",driver.getCurrentUrl());
            saveDataToES.saveData(jsonObject);
        }
        try {
            log.info("当前页--{}", driver.getCurrentUrl());

            WebElement webElement = driver.findElementByXPath("//*[@class=\"classPage\"]");
            webElement.click();
            Thread.sleep(3000);

        } catch (Exception e) {
            log.error("点击下一页出错--{}", e.getMessage());
        }

        partClickNext(driver, elements);

    }
}
